<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
    style="padding: 20px 30px 0 0;">

    <script type='text/html' template lay-done='layui.data.formdone(d.params);'>
        <input type='hidden' name='PID' id='PID' value="{{ d.params.PID || '' }}"/>
    </script>

    <div class='layui-form-item'>
        <label class='layui-form-label'>店铺选择</label>
        <div class="layui-input-inline">
            <select name='storeID' id='storeID' xm-select="storeID" xm-select-search="" lay-search>
                <option value=""></option>
            </select>
        </div>
    </div>

    <div class='layui-form-item'>
        <label class='layui-form-label'>数量</label>
        <div class='layui-input-block'>
            <input type="text" name="num" id="num" placeholder="数量" lay-verify='number' autocomplete="off"
                class="layui-input">
        </div>
    </div>

    <div class='layui-form-item'>
        <label class='layui-form-label'>会员等级 </label>
        <div class='layui-input-block'>
            <div class='layui-input-inline'>
                <!-- <select name='Level' id='Level' lay-search>
                </select> -->
                <select name='Level' id='Level' xm-select="Level" xm-select-search="" lay-search>
                    <option value=""></option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <input type="button" id="submit" lay-filter="layuiadmin-app-form-submit" value="确认" class="layui-btn">
        </div>
    </div>

</div>

<script type="text/javascript">

    layui.use(['form', 'admin', 'upload', 'laydate', 'formSelects'], function () {

        var form = layui.form;//required（必填项）phone（手机号）email（邮箱）url（网址）number（数字）date（日期）identity（身份证） 自定义值
        var formSelects = layui.formSelects;
        var admin = layui.admin;
        var $ = layui.$;



        formSelects.config('storeID', {
            keyName: 'Name',            //自定义返回数据中name的key, 默认 name
            keyVal: 'PID',            //自定义返回数据中value的key, 默认 value
        });

        formSelects.config('Level', {
            keyName: 'LevelStr',            //自定义返回数据中name的key, 默认 name
            keyVal: 'Level',            //自定义返回数据中value的key, 默认 value
        });


        admin.req({
            url: layui.setter.APIUrl + '/shop/Goods_memberpriceMgr/getLevels', //实际使用请改成服务端真实接口
            type: 'post',
            done: function (res) {

                // layui.each(res.Data, function (index, eachdata) {
                //     var strIsSel = '';
                //     $("#Level").append("<option value='" + eachdata.Level + "' " + strIsSel + ">" + eachdata.LevelStr + "</option>");
                // });


                layui.formSelects.data('Level', 'local', {
                    arr: res.Data,
                    //linkage: true,
                    //linkageWidth: 130
                });


                form.render('select');
            }
        });


        $("#submit").click(function () {

            var couponId = $("#PID").val();
            var level = formSelects.value('Level', 'valStr');//$("#Level").val();

            if (!couponId) {
                return layer.msg("请选择优惠券");
            }

            layer.load(); //上传loading
            admin.req({
                url: layui.setter.APIUrl + '/shop/CouponMgr/sendCoupon', //实际使用请改成服务端真实接口
                type: 'post',
                data: { couponId: couponId, level: level },
                done: function (res) {
                    layer.closeAll('loading'); //关闭loading
                    layer.closeAll();
                    layer.msg(res.ResultInfo);
                    //table.reload('LAY-app-content-list');
                }
            });

        });
    });


    layui.data.formdone = function (d) {
        var $ = layui.$;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;


        if (d.Type_Sel == 1) {
            $("#storeID").hide();
        }

        admin.req({
            url: layui.setter.APIUrl + '/shop/StoreMgr/customerQueryC', //实际使用请改成服务端真实接口
            type: 'post',
            data: { couponid: d.PID },
            done: function (res) {
                layui.formSelects.data('storeID', 'local', {
                    arr: res.Data,
                });
            }
        });

    }

</script>